package softtech.apps.utility;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Base64;

/**
 * task register with server when actived
 * @author tdinhphuoc
 * created date: 16-9-2014
 */
public class AsyncRegisterWithServer extends AsyncTask<Void, Void, Void>{
	
	final String EMAIL = "email";
	
	final String DEVICED_ID = "device_id";
	
	private String base64;
	
	Context mContext;
	
	String server_result;
	
	public AsyncRegisterWithServer(Context context , String email , String device_id){
		mContext = context;
		server_result = "";
		// init params to register with server
		try{
			JSONObject jObject = new JSONObject();
			jObject.put(EMAIL, email);
			jObject.put(DEVICED_ID , device_id);
			
			byte[] data = jObject.toString().getBytes("UTF-8");
			
			base64 = Base64.encodeToString(data, Base64.DEFAULT);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
	}

	@Override
	protected Void doInBackground(Void... params) {
		// make request to server
		HttpRequest req = new HttpRequest();
		ArrayList<NameValuePair> pairList = new ArrayList<NameValuePair>();
		pairList.add(new BasicNameValuePair("data", base64));
		server_result = req.makeServiceCall(ApplicationConstants.URL_REGISTER_LICENSE , HttpRequest.POST, pairList);
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		// after register
	}
	
}
